import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from "@angular/router";


import {Observable} from "rxjs/Observable";
import {CheckUserService} from "./check-user.service";
import {Injectable} from "@angular/core";
import {UserAuthService} from "./manage/user-auth.service";

@Injectable()
export class UserLoginAuthService implements CanActivate{

    public isCheck:boolean=false;

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

        if(this.isCheck)
        {
            this.isCheck=false;
            return true;
        }

        return this.check.Check()
            .then(response=>{
                if(response.result){
                    this.userAuth.isCheck=true;
                    this.router.navigate(["manage"]);
                    return true;
                }
                return true;
            }).catch(ex=>{
                return true;
            });

    }


    constructor(private check:CheckUserService,private router:Router,private userAuth:UserAuthService){}

}